package start.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import start.model.User;
import start.service.IUserService;
import start.util.MD5;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

@RestController
public class UserCtrl {
	@Resource
	private IUserService service;
	@Autowired
	private StringRedisTemplate redisTemplate;
	
	@RequestMapping("/user/login")
	public User login(String username,String password,HttpServletRequest request,HttpServletResponse response) {
		
		//对密码进行md5加密
		MD5 md5=new MD5();
		String pwd = md5.md5crypt(password);
		String id = request.getSession().getId(); //获取唯一会话id
		User user = new User();
		user.setUsername(username);
		user.setPassword(pwd);
		user = service.find(user);
		
		if(user!=null&&user.getId()>0) {
//			this.mapper.insert(id, username);//将会话id与用户名存入Redis数据库
			redisTemplate.opsForValue().set(id,username,30*60, TimeUnit.SECONDS);
			response.setHeader("token", id);
		}
		return user;
	}
	
	
}
